/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

#include "main_ability_slice.h"
#include "ability_env.h"
#include "ability_manager.h"
#include "components/ui_label.h"
#include "components/ui_label_button.h"
#include "components/ui_image_view.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include<stdio.h>
#include "file.h"
#include "securec.h"

namespace OHOS
{
REGISTER_AS(MainAbilitySlice)

constexpr static int ROOT_VIEW_POSITION_X = 0;
constexpr static int ROOT_VIEW_POSITION_Y = 0;
constexpr static int ROOT_VIEW_WIDTH = 960;
constexpr static int ROOT_VIEW_HEIGHT = 480;
static constexpr uint16_t MAX_PATH_LENGTH = 512;
static const char* const BACK_ICON_PATH = "/catView/assets/entry/resources/base/media/ic_back.png";
static const char* const BACKGROUND00_PATH = "/catView/assets/entry/resources/base/media/cat00.jpg";
static const char* const BACKGROUND01_PATH = "/catView/assets/entry/resources/base/media/cat01.jpg";
static const char* const BACKGROUND02_PATH = "/catView/assets/entry/resources/base/media/cat02.jpg";
static char g_backIconAbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_backGround00AbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_backGround01AbsolutePath[MAX_PATH_LENGTH] = {0};
static char g_backGround02AbsolutePath[MAX_PATH_LENGTH] = {0};
void MainAbilitySlice::OnStart(const Want &want)
{
    printf("MainAbilitySlice::OnStart\n");
    AbilitySlice::OnStart(want);
    const char* pathHeader = GetSrcPath();
    if(sprintf_s(g_backIconAbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACK_ICON_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backIconAbsolutePath error");
        return;
    }
    if(sprintf_s(g_backGround00AbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACKGROUND00_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround00AbsolutePath error");
        return;
    }
    if(sprintf_s(g_backGround01AbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACKGROUND01_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround01AbsolutePath error");
        return;
    }
    if(sprintf_s(g_backGround02AbsolutePath,MAX_PATH_LENGTH,"%s%s",pathHeader,BACKGROUND02_PATH) < 0){
        printf("GalleryAbilitySlice::OnStart | g_backGround02AbsolutePath error");
        return;
    }
    backIcon = new UIImageView();
    backIcon->SetPosition(0, 0);
    backIcon->SetSrc(g_backIconAbsolutePath);
    backIcon->SetTouchable(true);
    backIcon->Resize(40,40);
    auto onClick = [this] (UIView& view, const Event& event) -> bool {
        TerminateAbility();
        return true;
    };
    backIconListener = new EventListener(onClick, nullptr);
    backIcon->SetOnClickListener(backIconListener);

    //设置滑动容器组件UISwipeView
    swipe = new UISwipeView(UISwipeView::HORIZONTAL);
    swipe->SetPosition(105,40);
    swipe->Resize(751,440);

    backgroud0 = new UIImageView();
    backgroud0->SetPosition(0,0);
    backgroud0->SetSrc(g_backGround00AbsolutePath);
    backgroud0->SetAutoEnable(false);
    backgroud0->Resize(751,440);
    

    backgroud1 = new UIImageView();
    backgroud1->SetPosition(0,0);
    backgroud1->SetSrc(g_backGround01AbsolutePath);
    backgroud1->SetAutoEnable(false);
    backgroud1->Resize(751,440);

    backgroud2 = new UIImageView();
    backgroud2->SetPosition(0,0);
    backgroud2->SetSrc(g_backGround02AbsolutePath);
    backgroud2->SetAutoEnable(false);
    backgroud2->Resize(751,440);

    swipe->Add(backgroud0);
    swipe->Add(backgroud1);
    swipe->Add(backgroud2);
    swipe->SetLoopState(true);

    rootView_ = RootView::GetWindowRootView();
    rootView_->SetPosition(ROOT_VIEW_POSITION_X, ROOT_VIEW_POSITION_Y);
    rootView_->Resize(ROOT_VIEW_WIDTH, ROOT_VIEW_HEIGHT);
    rootView_->Add(backIcon);
    rootView_->Add(swipe);

    SetUIContent(rootView_);
}

void MainAbilitySlice::OnStop()
{
    printf("MainAbilitySlice::OnStop\n");
    AbilitySlice::OnStop();
    if (backIcon != nullptr) {
        delete backIcon;
        backIcon = nullptr;
    }
    if (backIconListener != nullptr) {
        delete backIconListener;
        backIconListener = nullptr;
    }
    if (backgroud0 != nullptr){
        delete backgroud0;
        backgroud0 = nullptr;
    }
    if (backgroud1 != nullptr){
        delete backgroud1;
        backgroud1 = nullptr;
    }
    if (backgroud2 != nullptr){
        delete backgroud2;
        backgroud2 = nullptr;
    }
    if (swipe != nullptr){
        delete swipe;
        swipe = nullptr;
    }
    if (rootView_ != nullptr){
        RootView::DestoryWindowRootView(rootView_);
        rootView_=nullptr;
    }
}
} // namespace OHOS
